package com.farmpost.server.controller;


import com.farmpost.server.controller.entity.ResetPassWord;
import com.farmpost.server.pojo.RespBean;
import com.farmpost.server.pojo.UserInfo;
import com.farmpost.server.pojo.UserLogin;
import com.farmpost.server.service.IUserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 用户基本信息 前端控制器
 * </p>
 *
 * @author king
 * @since 2022-04-03
 */
@RestController
public class UserInfoController {

    @Autowired
    private IUserInfoService iUserInfoService;

    @ApiOperation(value = "用户进行注册")
    @PostMapping("/register")
    public RespBean register(@RequestBody UserLogin userLogin, HttpServletRequest request) {
        return iUserInfoService.registerUser(userLogin.getUsername(),userLogin.getUserPassword(),userLogin.getCode(),request);
    }

    @ApiOperation(value = "修改用户信息")
    @PostMapping("/updateUserInfo")
    public RespBean updateUserInfo(@RequestBody UserInfo userInfo) {
        String username = userInfo.getUsername();
        String sex = userInfo.getSex() == null ? "" : userInfo.getSex();
        int age = (int)(userInfo.getAge() == null ? 0 : userInfo.getAge());
        String other = userInfo.getOther() == null ? "" : userInfo.getOther();
        return iUserInfoService.updateUserInfo(username, sex, age, other);
    }

    @ApiOperation(value = "用户头像上传")
    @PostMapping("/uploadUserFace")
    public RespBean uploadUserFace(@RequestPart MultipartFile file) throws Exception {
       return iUserInfoService.uploadUserFace(file);
    }

    @ApiOperation(value = "修改密码")
    @PostMapping("/modifyPassWord")
    public RespBean modifyPassWord(@RequestBody ResetPassWord resetPassWord) {
        return iUserInfoService.modifyPassWord(resetPassWord);
    }
}
